package com.liyuxiang.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyuxiang.yygh.enums.RefundStatusEnum;
import com.liyuxiang.yygh.model.order.PaymentInfo;
import com.liyuxiang.yygh.model.order.RefundInfo;
import com.liyuxiang.yygh.order.mapper.RefundInfoMapper;
import com.liyuxiang.yygh.order.service.RefundInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        LambdaQueryWrapper<RefundInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RefundInfo::getOrderId, paymentInfo.getOrderId());
        queryWrapper.eq(RefundInfo::getPaymentType, paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isEmpty(refundInfo)) {
            return refundInfo;
        }
        refundInfo = new RefundInfo();
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setUpdateTime(new Date());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setTradeNo(paymentInfo.getTradeNo());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
